
package com.vfa.vocaburay;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.vfa.data.Category;
import com.vfa.data.CategoryData;

public class DeleteCategory extends Activity {
        private Button cancelButton;
        private Button deleteButton;
        private EditText categotyText;
        public ListView listView;
        private int idelete = -1;
        private CategoryData category;
        public static final int MAX_LENGHT = 10;
        private ArrayAdapter<String> speechAdapter;
        private ArrayList<String> categoryInit  = new ArrayList<String> (MAX_LENGHT);
     @Override
	public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listview);
            cancelButton = (Button) findViewById(R.id.insertButton);
            cancelButton.setText("Delete");
            deleteButton = (Button) findViewById(R.id.cancelButton);
            deleteButton.setText("cancel");
            categotyText = (EditText) findViewById(R.id.categorytext);
            category = new CategoryData(getApplicationContext());
            listView = (ListView) findViewById(R.id.ListView001);
            listView.setClickable(true);
            ArrayList<Category>categoryName = new ArrayList<Category >();
            categoryName = category.getDatabase();
            for(int i=0;i<categoryName.size();i++){
             if(categoryName.get(i)!= null){
                 categoryInit.add(categoryName.get(i).getCategoryName());
                 Log.e("LocNNT", "test du lieus ne =========================="+categoryName.get(i).getCategoryName());
             }
         }
            speechAdapter = new ArrayAdapter<String>(this, R.layout.listiterm, categoryInit);
            listView.setAdapter(speechAdapter);
            listView.getCheckedItemPosition();
            Log.e("LocNNT", "test du lieus ne =========================="+listView.getCheckedItemPosition());

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                  @Override
                  public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                    Object o = listView.getItemAtPosition(position);
                    Log.e("LocNNT", "test xem chay vao chua ");
                    Log.e("LocNNT", "test du lieus ne =========================="+ o.toString());
                    Log.e("LocNNT", "test du lieus ne =========================="+ position);
                    idelete = position;
                    arg1.setBackgroundColor(0xfffff000);
                  }

                });

            cancelButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.e("LocNNT", "test du lieus ne =========================="+listView.getSelectedItemId());
                    Log.e("LocNNT", " idelete     ne =========================="+ idelete);
                    if(idelete != -1&& idelete>= 0 && idelete<= categoryInit.size()-1){
                        updateListView(idelete);
                    }
                    // Perform action on click
                    View v1 = listView.getFocusedChild();
                   View v222 =  listView.findFocus();
                   Log.e("LocNNT", "test view duoc chon ne=================="+listView.getSelectedItemId() );
                    if(v1!= null){

                         Log.e("LocNNT", "test view duoc chon ne=================="+listView.getSelectedItemId() );
                    }
                    else{
                        Log.e("LocNNT", "null mat roi ne ================================" );
                    }

                }

            });
            deleteButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click
                    Intent intent = new Intent(DeleteCategory.this, CategoryActivity.class);
                    startActivity(intent);
                    DeleteCategory.this.finish();
                }
            });
     }
     private boolean checkCategoryAvailable(String name) {
         // TODO Auto-generated method stub
         ArrayList<Category >categoryName = new ArrayList<Category >();
         categoryName = category.getDatabase();
         for(int i=0;i<categoryName.size();i++){
              if(name.equals(categoryName.get(i).getCategoryName())){
                  return true;
              }
         }
         return false;
     }
     private void updateListView(int position) {
         // TODO Auto-generated method stub
         idelete = -1;
         category.delete(categoryInit.get(position).toString());
         int tempSize = categoryInit.size();
         for(int i =0;i<tempSize;i++){
             if(i == position){
                 categoryInit.remove(position);
             }
         }
         speechAdapter = new ArrayAdapter<String>(this, R.layout.listiterm, categoryInit);
         listView.setAdapter(speechAdapter);
     }

}
